package com.edu.cqr.web.demo.service.impl;

import com.edu.cqr.web.demo.dto.FreeRoomDTO;
import com.edu.cqr.web.demo.dto.RoomOrderDTO;
import com.edu.cqr.web.demo.dto.RoomOrderDetailDTO;
import com.edu.cqr.web.demo.mapper.FreeRoomCalendarMapper;
import com.edu.cqr.web.demo.mapper.RoomOrderDetailMapper;
import com.edu.cqr.web.demo.mapper.RoomOrderMapper;
import com.edu.cqr.web.demo.mapper.RoomTypeMapper;
import com.edu.cqr.web.demo.model.FreeRoomCelendar;
import com.edu.cqr.web.demo.model.RoomOrderDetail;
import com.edu.cqr.web.demo.model.RoomPrice;
import com.edu.cqr.web.demo.model.RoomType;
import com.edu.cqr.web.demo.service.RoomService;
import com.edu.fjzzit.web.myhotel.model.RoomOrder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@Service
public class RoomServicelmpl implements RoomService {
    @Resource
    private FreeRoomCalendarMapper freeRoomCelendarMapper;

    @Resource
    private RoomOrderMapper roomOrderMapper;

    @Resource
    private RoomOrderDetailMapper roomOrderDetailMapper;

    @Resource
    private  RoomTypeMapper roomTypeMapper;

    @Override
    public List<FreeRoomDTO> findFreeRoomByMinFreeCount(String checkInDate, String checkOutDate) {
        List<FreeRoomCelendar> freeRoom = freeRoomCelendarMapper.findFreeRoomByMinFreeCount(checkInDate, checkOutDate);
        freeRoomCelendarMapper.findFreeRoomByMinFreeCount(checkInDate, checkOutDate);
        List<FreeRoomDTO> freeRoomDTOS = new ArrayList<>();
        for (FreeRoomCelendar freeRoomlist : freeRoom) {
            FreeRoomDTO freeRoomDTO = new FreeRoomDTO();
            Long roomTypeNum = freeRoomlist.getRoomTypeNum();
            //根据房间类型主键查询房间类型表
            RoomType roomTypeByRoomTypeNum = findRoomTypeByRoomTypeNum(roomTypeNum);
            //根据房间类型主键查询房间价格表
            RoomPrice roomPriceByRoomTypeNum = findRoomPriceByRoomTypeNum(roomTypeNum);
            freeRoomDTO.setFreeCount(freeRoomlist.getFreeCount());
            freeRoomDTO.setBedType(roomTypeByRoomTypeNum.getBedType());
            freeRoomDTO.setBreakfastType(roomPriceByRoomTypeNum.getBreakfastType());
            freeRoomDTO.setRoomPrice(roomPriceByRoomTypeNum.getRoomPrice());
            freeRoomDTO.setRoomPriceName(roomPriceByRoomTypeNum.getRoomPriceName());
            freeRoomDTO.setRoomTypeName(roomTypeByRoomTypeNum.getRoomTypeName());
            freeRoomDTOS.add(freeRoomDTO);
        }
        return freeRoomDTOS;
    }

    @Override
    public com.edu.cqr.web.demo.model.RoomType findRoomTypeByRoomTypeNum(Long roomTypeNum) {
        return freeRoomCelendarMapper.findRoomTypeByRoomTypeNum(roomTypeNum);
    }

    @Override
    public RoomPrice findRoomPriceByRoomTypeNum(Long roomTypeNum) {
        return freeRoomCelendarMapper.findRoomPriceByRoomTypeNum(roomTypeNum);
    }
    @Override
    @Transactional
    public Long reserveRoom(RoomOrderDTO roomOrderDTO) {
        RoomOrder roomOrder = new RoomOrder();
        String customerName = roomOrderDTO.getCustomerName();
        roomOrder.setCustomerName(customerName);
        roomOrder.setCusomerPhone(roomOrderDTO.getCustomerPhone());
        roomOrder.setRoomOrderState(Byte.parseByte("0"));
        roomOrderMapper.insert(roomOrder);
        RoomOrderDetail roomOrderDetail = new RoomOrderDetail();
        int index = 0;
        String checkInDateStr = null;
        String checkOutDateStr = null;
        //订房间数
        Integer roomCount = 0;
        //房间类型
        Long roomTypeNum = null;
        //获取生成的订单的流水号
        Long roomOrderNum = roomOrderMapper.selectRoomOrderNumByCustomerName(customerName);
        for (RoomOrderDetailDTO roomOrderDetailDTOS : roomOrderDTO.getRoomOrderDetailDTOList()) {
            //字符串转为日期格式
            checkInDateStr = roomOrderDetailDTOS.getCheckInDate();
            checkOutDateStr = roomOrderDetailDTOS.getCheckOutDate();

            roomTypeNum = roomTypeMapper.findRoomTypeNumByRoomTypeName(roomOrderDetailDTOS.getRoomTypeName());
            roomOrderDetail.setRoomTypeNum(roomTypeNum);

            roomOrderDetail.setRoomOrderNum(roomOrderNum);
            roomOrderDetail.setRoomPriceName(roomOrderDetailDTOS.getRoomPriceName());
            roomOrderDetail.setBreakfastType(roomOrderDetailDTOS.getBreakfastType());
            roomOrderDetail.setRoomPrice(roomOrderDetailDTOS.getRoomPrice());

            roomCount = roomOrderDetailDTOS.getRoomCount();
            roomOrderDetail.setRoomCount(roomCount);

            roomOrderDetail.setCheckInDate(getNowDate(checkInDateStr));
            roomOrderDetail.setCheckOutTime(getNowDate(checkOutDateStr));
            roomOrderDetail.setRoomOrderDetailPrice(roomOrderDetailDTOS.getRoomPrice() * roomOrderDetailDTOS.getRoomCount());
            index = roomOrderDetailMapper.insert(roomOrderDetail);
            System.out.println("index---?>" + index);
        }
        if (index == 1) {
            //修改客房余数
            freeRoomCelendarMapper.updFreeCountByRoomCount(checkInDateStr, checkOutDateStr, roomCount, roomTypeNum);
        }
        return roomOrderNum;
    }

    /**
     * 字符串转为日期格式
     *
     * @param dateString 日期字符串
     * @return 日期
     */
    public java.sql.Date getNowDate(String dateString) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
        Date currentTime_2 = null;
        try {
            currentTime_2 = (Date) formatter.parse(dateString);
        } catch (ParseException e) {
        }
        java.sql.Date resultDate = new java.sql.Date(currentTime_2.getTime());
        System.out.println("checkOutDateStr------>" + resultDate);
        return resultDate;
    }
}